﻿
//相似词检索
jQuery.support.cors = true;
function smartKeyup(e) {
    var availableTags = [];
    var obj = $(e.target);
    var tag = $("#smartwordtag").val()
    var para = $("#smartaddress").val() + "?tag=" + tag + "&key=" + encodeURI(obj.val());
    $.ajax({
        type: "get",
        url: para,
        crossDomain: true,
        success: function (msg) {
            if (msg != "") {
                availableTags = msg.split(";");
                obj.autocomplete({
                    source: availableTags
                });
            }
        },
        error: function (e)
        { }
    })
}
function ForceWindow() {
    this.r = document.documentElement;
    this.f = document.createElement("FORM");
    this.f.target = "_blank";
    this.f.method = "post";
    this.r.insertBefore(this.f, this.r.childNodes[0]);
}

ForceWindow.prototype.open = function (sUrl) {
    this.f.action = sUrl;
    this.f.submit();
}
//点击下载时，先判断文件是否存在然后下载  href 下载路径 fileid 为-1时代表是单文件下载
function isDown(dbID, recordId, MediaType, urlstr, fileid, downtype) {
    $.ajax({
        async: false,
        type: "GET",
        url: urlstr,
        data: { "dbID": dbID, "recordId": recordId, "MediaType": MediaType,"fileid":fileid ,"downType":downtype},
        success: function (msg) {
            var result = eval(msg);
            if (result[0].status == "1") {
                window.location.href = result[0].url;
            }
            else if (result[0].status == "2") {
                var w = window.open();
                w.location = result[0].url;
            }
            else {
                messageAlert(result[0].status);
            }
        },
        error: function (e) {
            messageAlert("系统异常，请稍后后重试");
        }
    })
}
//点击在线预览时，先判断文件是否存在然后预览  href 路径
function isView(dbID, dbName, recordId, MediaType, urlstr, fileid, downType) {

    $.ajax({
        async: false,
        type: "GET",
        url: urlstr,
        data: { "dbID": dbID, "dbName": dbName, "recordId": recordId, "MediaType": MediaType, "fileid": fileid, "downtype": downType },
        success: function (msg) {
            var result = eval(msg);
            if (result[0].status == "1" || result[0].status == "2") {
                var w = window.open();
                w.location.href = result[0].url;
            }
            else {
                messageAlert(result[0].status);
            }
        },
        error: function (e) {
            messageAlert("系统异常，请稍后后重试");
        }
    })
}

function isTopicView(dbID, dbName, recordId, MediaType, urlstr, fileid, downType) {

    $.ajax({
        async: false,
        type: "GET",
        url: urlstr,
        data: { "dbID": dbID, "dbName": dbName, "recordId": recordId, "MediaType": MediaType, "fileid": fileid, "downtype": downType },
        success: function (msg) {
            var result = eval(msg);
            if (result[0].status == "1" || result[0].status == "2") {
                var w = window.open();
                w.location.href = "/TopicView/Index?dbName=" + dbName + "&resourceId=" + recordId + "&recordId=" + recordId + "&dbId=" + dbID + "&downType=" + downType
            }
            else {
                messageAlert(result[0].status);
            }
        },
        error: function (e) {
            messageAlert("系统异常，请稍后后重试");
        }
    })
}

//列表页文件下载 获取记录对应的文件信息
function downfilelist(dbID, recordID, dbName, type, isexist, getfileurl, downtype) {
    $.ajax({
        type: "GET",
        url: timestamp(getfileurl),
        data: { "recordID": recordID, "dbName": dbName, "mediaType": type, "dbID": dbID,"downtype":downtype },
        success: function (msg) {
            if (msg != null && msg!="") {
                var len = eval(msg).length;
                if (len > 1) {
                    $(".video-download").html("");
                    $.each(msg, function (index, entry) {//ajax回调后,类似于for循环.
                        var listr = "<li>" + entry['FILENAME'] +
                            "<a class='btn btn-m btn-prim-f posa'" +
                            "onclick=\"javascript:isDown('" + dbID + "','" + recordID + "','" + type
                            + "','" + isexist + "','" + entry['SYSID'] + "','" + downtype + "')\"" +
                            "style='margin-right:40px'>点击下载 </a></li>";
                        $(".video-download").append(listr);
                    });
                    showdown();
                }
                else if (len == 1) {
                    closedown();
                    isDown(dbID, recordID, type, isexist, eval(msg)[0]['SYSID'],downtype);//只有一个文件则直接下载
                }
            } else {
                messageAlert("文件不存在");
            }
        },
        error: function (e) {
            messageAlert("系统异常，请稍后后重试");
        }
    })
}

//下载弹窗关闭
function closedown() {
    // 点击关闭
    $('.talkDivBg').hide();
    $('.Down').hide();
}
//显示下载弹窗
function showdown() {
    $('.talkDivBg').show();
    $('.Down').css({
        'margin-top': -($(".Down").height() / 2)
    });
    $('.Down').show();

    //$('.talkDivBg').show();
    //$('.Down').css({
    //    'margin-top': -($(".Down").height() / 2),
    //    'margin-left': -($(".Down").width() / 2)
    //});
    //$('.Down').show();
    //$('.btn').click(function () {
    //    $('.talkDiv').eq($(this).index()).show();
    //});
}

//在线预览弹窗关闭
function closeview() {
    // 点击关闭
    $('.talkDivBg').hide();
    $('.View').hide();

}
//显示在线预览弹窗
function showview() {

    $('.talkDivBg').show();
    $('.View').show();
}
//浏览器缓存
function timestamp(url) {
    var getTimestamp = new Date().getTime();
    if (url.indexOf("?") > -1) {
        url = url + "&t=" + getTimestamp
    } else {
        url = url + "?t=" + getTimestamp
    }
    return url;
}
//过滤非法字符
function regtext(value) {
    var reg = /^[^~@#$%^&*<>]+$/;
    if (value != "" && !reg.test(value)) {
        return false;
    }
    else {
        return true;
    }
}
//关键词过滤非法字符
function regKeyWord(value) {
    var reg = /^[^~?!@#$%^&*<>,.；]+$/;
    if (value != "" && !reg.test(value)) {
        return false;
    }
    else {
        return true;
    }
}
function regemail(value) {
    var reg = /^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/;
    if (value != "" && !reg.test(value)) {
        return false;
    }
    else {
        return true;
    }
}

//消息框
function messageAlert(msg) {
    $("#dialog-message").html("<p style='padding:20px' class='message'>" + msg + "</p>");
    $("#dialog-message").dialog({
        modal: true,
        resizable: false,
        buttons: {
            '确认': function () {
                $(this).dialog("close");
            }
        }
    });
}
//操作提示框
function confirmDialog(msg, callback) {
    $("#dialog-confirm").html("<p style='padding:20px' class='message'>" + msg + "</p>");
    $("#dialog-confirm").dialog({
        resizable: false,
        modal: true,
        height: 200,
        buttons: {
            '确认': function () {
                callback.call();
                $(this).dialog('close');
            },
            '取消': function () {
                $(this).dialog('close');
            }
        }
    });
}

//操作提示框
function confirmDialogSure(msg, callback) {
    $("#dialog-confirm").html("<p style='padding:20px' class='message'>" + msg + "</p>");
    $("#dialog-confirm").dialog({
        resizable: false,
        modal: true,
        height: 200,
        buttons: {
            '确认': function () {
                callback.call();
                $(this).dialog('close');
            }
        }
    });
}
//过滤输入框非法字符
function reginputtext(value) {
    var reg = /['\"]+|\b(alert|confirm|prompt)\b|^\+\/v(8|9)|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|\/\*.+?\*\/|<\s*script\b|<\s*img\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)/;
    if (!reg.test(value)) {
        return false;
    }
    else {
        return true;
    }
}